package com.Controller;
import com.alibaba.fastjson.JSON;
import com.mysql.cj.util.StringUtils;
import com.pojo.SysUser;
import com.service.user.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;


@Controller
@RequestMapping(value = "/user")
public class UserController {
    @Autowired
    private SysUserService sysUserService;

    @PostMapping(value = "/login")
    public String login(Model model, HttpServletRequest request,String account, String password){
        SysUser sysUser = sysUserService.login(account, password);

        //用户名密码不正确
        if (sysUser == null){
            model.addAttribute("error","用户名或密码不正确");
            return "login";
        }
        /**知识点，Session*/
        request.getSession().setAttribute("userSession",sysUser);

        return "redirect:/frame";
    }

    @RequestMapping(value = "/list")
    public String list(Model model,String realName,@RequestParam(defaultValue = "1",required = false)  int pageIndex){
        List<SysUser> list = sysUserService.list(realName,pageIndex);
        int totalCount = sysUserService.count(realName);
        int totalPageCount = totalCount / 3;
        if (totalCount % 3 > 0){
            totalPageCount ++;
        }
        if (pageIndex>totalPageCount){
        pageIndex=totalPageCount;
        }
        model.addAttribute("totalCount",totalCount);
        model.addAttribute("list",list);
        model.addAttribute("realName",realName);
        model.addAttribute("totalPageCount",totalPageCount);
        model.addAttribute("currentPageNo",pageIndex);
        return "sysUser/list";
    }

    //跳转到添加页面
    @RequestMapping("/toAdd")
    public String toAdd(){
        return "/sysUser/add";
    }

    @RequestMapping("/add")
    //添加数据
    public String add(SysUser sysUser) {
        sysUserService.add(sysUser);
        return "redirect:/user/list";
    }

    @RequestMapping("/delete")
    public String delete(int id){
        sysUserService.delete(id);
        return "redirect:/user/list";
    }

    @RequestMapping(value = "/SelectId")
    public Object toSelectId(int id){
        SysUser sysUser = null;
           try {
                sysUser = sysUserService.sysUser(id);
           }catch (Exception e){
               e.printStackTrace();
               return "failed";
           }
       return sysUser;
    }

    @RequestMapping("/toUpdate")
    public String toUpdate(Model model,int id){
        SysUser sysUser = sysUserService.sysUser(id);
        model.addAttribute("sysUser",sysUser);
        return "/sysUser/update";
    }

    @RequestMapping("/update")
    public String update(Model model,SysUser user){
        int i = sysUserService.update(user);
        if (i > 0){
            return "redirect:/user/list";
    }
        model.addAttribute("sysUser",user);
        return "/sysUser/update";
    }

    @ResponseBody
    @RequestMapping("/userExist")
    public Object userExist(@RequestParam String account){
        System.out.println("验证用户名account="+account+"的用户是否存在");
        HashMap<String,Object> resultMap = new HashMap<String, Object>();
        if (StringUtils.isNullOrEmpty(account)){
            resultMap.put("exist",1);
        }else {
            SysUser sysUser = sysUserService.getAccountExist(account);
            if (null != sysUser){
                resultMap.put("exist",1);
            }else {
                resultMap.put("exist",0);
            }
        }
        return JSON.toJSONString(resultMap);
    }

    //跳转到修改密码页面
    @RequestMapping("/toupdatePassword")
    public String toupdatePassword(){
        return "sysUser/upadtePassword";
    }

    //处理修改密码
    @RequestMapping("/updatePassword")
    public String updatePassword(Model model,@RequestParam String account,@RequestParam String password){
        int result = sysUserService.updatePassword(account,password);
        if (result > 1){
            model.addAttribute("info","修改成功");
        }else {
            model.addAttribute("info","修改失败");
        }
        return "redirect:/user/upadtePassword";
    }
}
